package com.example.moments.adapter;
//package en.edu.zucc.pb.recyclerview;

import android.annotation.SuppressLint;
//import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.example.moments.MainActivity;
import com.example.moments.R;
import com.example.moments.starpMome.Moment;
import com.example.moments.utils.DrawUtil;
import com.example.moments.widget.NineGridView;
//import com.example.myapplication.R;
//import com.example.myapplication.model.Moment;
//import com.example.myapplication.nineGrid.NineGridView;
//import com.example.myapplication.nineGrid.NineImageAdapter;
//import com.example.myapplication.util.DrawUtil;
//import com.example.myapplication.widget.NineGridView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * RecyclerView的适配器
 */

public class RvAdapter extends RecyclerView.Adapter<RvAdapter.MyViewHolder> {
    private Context context;
//    private List<Integer> datas;
    private List<Moment> datas;

    /**
     * item的点击事件的长按事件接口
     */
    private OnItemClickListener onItemClickListener;
    /**
     * 瀑布流时的item随机高度
     */
    private List<Integer> heights = new ArrayList<>();

    /**
     * 不同的类型设置item不同的高度
     *
     * @param type
     */

    private int type = 0;

    public RvAdapter(Context context, List<Moment> datas) {
        this.context = context;
        this.datas = datas;
        for (Moment i : datas) {
            int height = (int) (Math.random() * 100 + 300);
            heights.add(height);
        }
    }




    public void setType(int type) {
        this.type = type;
    }

    /**
     * 设置点击事件
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View contentView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        View contentView = LayoutInflater.from(context).inflate(R.layout.moment_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(contentView);
        return viewHolder;
    }

    private static final String TAG = "RvAdapter";

//    所有的主要的 没有头部吗
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
//        RecyclerView.LayoutParams layoutParams;
//        if (type == 0) {
//            layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        } else if (type == 1) {
//            layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        } else {
//            layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, heights.get(position));
//            layoutParams.setMargins(2, 2, 2, 2);
//        }
//        holder.itemView.setLayoutParams(layoutParams);
//        content 如果是 图片呢
//        RecyclerView 不显示
//        new Intent(context, MainActivity.class)
        Log.i(TAG,"position  "+ position);
        Moment moment = datas.get(position);
//        Log.i("onBindViewHolder", String.valueOf(moment));
        Log.i(TAG,"onBindViewHolder  "+ moment);
//        holder.writtenWords.setImageResource(datas.get(position));
        holder.writtenWords.setText(moment.getWrittenwords());
//        holder.tvDate.setText(moment.get());
        holder.name.setText(moment.getNickname());
//        holder.btnPopup.setText(moment.getNickname());
//        DrawUtil.addButtonIcon( holder.btnPopup,R.drawable.ic_pig_nose,"吃饭",context);
//        没有画图啊
//        DrawUtil.addButtonIcon( holder.btnPopup, R.drawable.ic_pig_nose,"",context);
//        holder.avatar.setImageResource(moment.getAvatar());
//        holder.avatar.setImageResource(moment.getAvatar());
        Resources resources = context.getResources();
        DrawUtil.setRoundImageDrawable( holder.avatar,moment.getAvatar(),resources);
//        moment.getCreattime()
//        holder.tv.setText("分类" + position);
        /**设置item点击监听**/
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
//                    onItemClickListener.onItemClickListener(position, datas.get(position));
                    onItemClickListener.onItemClickListener(position, moment.getId());
                }
            }
        });

//        Moment.bui
        String imgs = moment.getImgs();
//        String[] split = imgs.split(",")
//        Stream<String> stream = Arrays.stream(imgs.split(","));
//        List<Object> collect1 = stream.collect(Collectors.toList());
//        图片 分割了 根据 ","
        List<String> collect = Arrays.stream(imgs.split(",")).collect(Collectors.toList());
//        List<Object> collect = Arrays.stream(imgs.split(",")).collect(Collectors.toList());
//        java7  只能是    List<Object>
//        collect.forEach(o->(String)o);
        NineImageAdapter nineImageAdapter = new NineImageAdapter(context, collect);
        holder.nineGridView.setAdapter(nineImageAdapter);


//        get
//        RequestOptions mRequestOptions = new RequestOptions().centerCrop();
//        DrawableTransitionOptions mDrawableTransitionOptions = DrawableTransitionOptions.withCrossFade();
//        holder.nineGridView.setAdapter(new NineImageAdapter(context,
//                mRequestOptions, mDrawableTransitionOptions, result));


//                m可绘制过渡选项  mDrawableTransitionOptions
//        holder.nineGridView.setOnImageClickListener(new NineGridView.OnImageClickListener() {
//            @Override
//            public void onImageClick(int position, View view) {
//                if (iwHelper != null) {
//                    iwHelper.show((ImageView) view, imgViewHolder.nineGridView.getImageViews(),
//                            tempYuantu);
//                }
//                if (expandFoldListener != null) {
//                    //返回主页去弹出评论
//                    expandFoldListener.imageOnclick();//刷新
//
//                }
//            }
//        });

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    /**
     * 用于缓存的ViewHolder
     */
    public class MyViewHolder extends RecyclerView.ViewHolder {
//        private ImageView imageView;
        public TextView writtenWords;
        public TextView tvDate;
        public TextView name;
        public ImageView avatar;
//        public Button btnPopup;
        public View btnPopup;
        public NineGridView nineGridView;

        public MyViewHolder(View itemView) {
            super(itemView);
            name =itemView.findViewById(R.id.name);
            writtenWords = itemView.findViewById(R.id.writtenWords);
            avatar = itemView.findViewById(R.id.avatar);
            btnPopup = itemView.findViewById(R.id.btnPopup);
            tvDate = itemView.findViewById(R.id.tvDate);
            nineGridView = itemView.findViewById(R.id.nineGridView);

//            Resources resources = context.getResources();
////            Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.makima);
//            Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.miku_fang);
//            Drawable drawable = DrawUtil.roundBitmapByBitmapDrawable(bitmap,
//                    100,
//                    100,
//                    5, resources);
//            avatar.setImageDrawable(drawable);
//            这里设置不行 ，外面 还是设置了
        }
    }

    /**
     * 设置item监听的接口
     */
    public interface OnItemClickListener {
        void onItemClickListener(int position, Integer data);

    }
}
